package kr.co.iniline.rmkb.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Mesh;
import com.badlogic.gdx.graphics.VertexAttribute;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;

public class Background {
	GL10 gl = null;
	Mesh backMesh = null;
	
	public Background() {
		gl = Gdx.gl10;
		float backUV[] = Resource.getUV("background");
		backMesh = new Mesh(true, 4, 0, new VertexAttribute(Usage.Position, 2, "a_position"), new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoord1"));
		backMesh.setVertices(new float[]{
			0f, 0f, backUV[0], backUV[1],
			0f, 1f, backUV[2], backUV[3],
			1f, 1f, backUV[4], backUV[5],
			1f, 0f, backUV[6], backUV[7]
		});
	}
	
	public void render(float delta) {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glOrthof(0, 1, 0, 1, 0, 1);
		
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		
		// No depth buffer writes for background.
		gl.glDepthMask( false );
		
//		backTexture.bind();
		gl.glEnable(GL10.GL_TEXTURE_2D);
		Resource.bindTexture("background");
		Resource.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
		Resource.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		backMesh.render(GL10.GL_TRIANGLE_FAN);
		gl.glEnable(GL10.GL_TEXTURE_2D);
	
//		gl.glBindTexture( GL10.GL_TEXTURE_2D, textureId[4] );
//		gl.glBegin( GL10.GL_QUADS ); {
//
//			gl.glTexCoord2f( 0f, 0f );
//		  gl.glVertex2f( 0, 0 );
//		  gl.glTexCoord2f( 0f, 1f );
//		  gl.glVertex2f( 0, 1f );
//		  gl.glTexCoord2f( 1f, 1f );
//		  gl.glVertex2f( 1f, 1f );
//		  gl.glTexCoord2f( 1f, 0f );
//		  gl.glVertex2f( 1f, 0 );
//		} gl.glEnd();
	
		gl.glDepthMask( true );
	
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
	}
}
